import selenium
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from enum import Enum

class DriverType(Enum):
    CHROME = 10
    FIREFOX = 20
    EDGE = 30




# 驱动管理器
class DriverManager:
    def __init__(self,driver_id:DriverType=DriverType.CHROME,driver_path=None):
        if driver_id == DriverType.CHROME:
            # 禁用日志
            options = webdriver.ChromeOptions()
            options.add_experimental_option(
                'excludeSwitches', ['enable-logging'])
            
            self.driver = webdriver.Chrome(options=options,service=ChromeService(driver_path))
        elif driver_id == DriverType.FIREFOX:
            self.driver = webdriver.Firefox(service=FirefoxService(driver_path))
        elif driver_id == DriverType.EDGE:
            self.driver = webdriver.Edge(service=EdgeService(driver_path))
        else :
            raise ValueError(f"不支持的浏览器类型{driver_id}")



# 元素定位方式
class FindElementType(Enum):
    ID = 0
    NAME = 1
    TAG = 2
    CLASS = 3
    LINK = 4
    PLINK = 5
    CSS = 6
    XPATH =7

class BasePage:
    def __init__(self,driver_id:DriverType=DriverType.CHROME,driver_path=None):
        self.driver = DriverManager(driver_id,driver_path).driver
    
    # 打开网页
    def open(self,url:str):
        self.driver.get(url)


    # 定位元素
    def find_element(self,type:FindElementType,value:str):

        if type == FindElementType.ID:
            el = self.driver.find_element(By.ID,value)
        elif type == FindElementType.NAME:
            el = self.driver.find_element(By.NAME,value)
        elif type == FindElementType.TAG:
            el = self.driver.find_element(By.TAG_NAME,value)
        elif type == FindElementType.CLASS:
            el = self.driver.find_element(By.CLASS_NAME,value)
        elif type == FindElementType.LINK:
            el = self.driver.find_element(By.LINK_TEXT,value)
        elif type == FindElementType.PLINK:
            el = self.driver.find_element(By.PARTIAL_LINK_TEXT,value)
        elif type == FindElementType.CSS:
            el = self.driver.find_element(By.CSS_SELECTOR,value)
        elif type == FindElementType.XPATH:
            el = self.driver.find_element(By.XPATH,value)
        else:
            raise ValueError(f"不支持的定位方式{type}")
        return el

    # 点击目标元素
    def click_element(self,type:FindElementType,value:str):

        self.find_element(type,value).click()
    
    # 向目标元素输入文本
    def input_data(self,type:FindElementType,value:str,text):
        self.find_element(type,value).send_keys(text)
    
    # 鼠标悬停
    def move_mouse_to_element(self,type:FindElementType,value:str,sec=1):
        ActionChains(self.driver).move_to_element(self.find_element(type,value)).pause(sec).perform()

    # 切换框架
    def switch_to_case(self,id_name_or_element):
        self.driver.switch_to.frame(id_name_or_element)

    # 切换窗口
    def switch_to_window(self,title):
        handles = self.driver.window_handles
        for handle in handles:
            self.driver.switch_to.window(handle)
            if self.driver.title == title:
                break

